// api/ApiService.ts
import customNotification from "@/components/Notification";
import { ApiResponse } from "@/pages/Login/types/d";
import request from "@/utils/request";

class HttpUtils {
  public static async post<T>(
    url: string,
    body?: Record<string, any>,
    options?: RequestInit
    // headers?: Record<string, any>,
  ): Promise<ApiResponse<T>> {
    return request<ApiResponse<T>>(url, {
      method: "POST",
      body: JSON.stringify(body),
      headers: {
        "Content-Type": "application/json",
      },
      ...options,
    });

    // return new Promise<ApiResponse<T>>(async (resolve, reject) => {
    //   try {
    //     const response = await request<ApiResponse<T>>(url, {
    //       method: "POST",
    //       body: JSON.stringify(body),
    //       headers: headers,
    //       ...options,
    //     });

    //     console.log(response);

    //     if (response.code === 200) {
    //       resolve(response); // 返回数据
    //     } else {
    //       // console.error(`API error: ${response.message}`);

    //       customNotification.error({
    //         message: "错误信息",
    //         description: response?.message || "未知错误",
    //       });

    //       // resolve(null); // 或者 reject(new Error(response.message));
    //       // reject(new Error(response.message));
    //     }
    //   } catch (error) {
    //     console.error(`Request failed: ${error}`);
    //     reject(error); // 处理请求失败
    //   }
    // });
  }

  public static async request<T>(
    url: string,
    method: string,
    body?: Record<string, any>
  ): Promise<ApiResponse<T>> {
    return request<ApiResponse<T>>(url, {
      method: method,
      headers: {
        "Content-Type": "application/json",
      },
      ...body,
    });
  }

  public static async get<T>(
    url: string,
    options?: RequestInit
  ): Promise<ApiResponse<T>> {
    return request<ApiResponse<T>>(url, {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
      },
      ...options,
    });
  }

  public static async put<T>(
    url: string,
    body?: Record<string, any>,
    options?: RequestInit
  ): Promise<ApiResponse<T>> {
    return request<ApiResponse<T>>(url, {
      method: "PUT",
      body: JSON.stringify(body),
      headers: {
        "Content-Type": "application/json",
      },
      ...options,
    });
  }

  public static async delete<T>(
    url: string,
    options?: RequestInit
  ): Promise<ApiResponse<T>> {
    return request<ApiResponse<T>>(url, {
      method: "DELETE",
      headers: {
        "Content-Type": "application/json",
      },
      ...options,
    });
  }
}

export default HttpUtils;
